<?php

declare(strict_types=1);

namespace App\Model;

use Aether\AetherModel;
use Aether\Contract\TreeableInterface;
use Aether\Traits\AetherTree;
use Carbon\Carbon;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\Relations\BelongsTo;
use Hyperf\Database\Model\Relations\HasMany;

/**
 * 校区模型（支持层级结构）.
 * @property int $id
 * @property string $name 校区名称
 * @property int $parent_id 父级ID
 * @property int $level 层级：1-省份，2-地级市，3-校区
 * @property string $province 省份
 * @property string $city 地级市
 * @property string $address 详细地址
 * @property string $contact_phone 联系电话
 * @property string $contact_person 联系人
 * @property int $status 状态：0-禁用，1-启用
 * @property Carbon $created_at
 * @property Carbon $updated_at
 * @property Carbon $deleted_at
 */
class Campus extends AetherModel implements TreeableInterface
{
    use AetherTree;
    protected ?string $table = 'da_campus';

    protected array $fillable = [
        'name',
        'parent_id',
        'level',
        'province',
        'city',
        'address',
        'contact_phone',
        'status',
    ];

    protected array $casts = [
        'id' => 'integer',
        'parent_id' => 'integer',
        'level' => 'integer',
        'status' => 'integer',
        'created_at' => 'datetime',
        'updated_at' => 'datetime',
        'deleted_at' => 'datetime',
    ];

    protected array $search = [
        'name' => 'like',
        'level' => '=',
        'parent_id' => '=',
        'city' => 'like',
        'province' => 'like',
        'status' => '=',
    ];

    protected array|bool|string $sortable = false;

    /**
     * 获取父级校区.
     */
    public function parent(): BelongsTo
    {
        return $this->belongsTo(Campus::class, 'parent_id', 'id')
            ->where('status', 1)
            ->whereNull('deleted_at');
    }

    /**
     * 获取子校区.
     */
    public function children(): HasMany
    {
        return $this->hasMany(self::class, 'parent_id', 'id');
    }

    /**
     * 按省份查询.
     */
    public function scopeProvince(Builder $query, string $province): Builder
    {
        return $query->where('province', 'like', '%' . $province . '%');
    }

    /**
     * 按城市查询.
     */
    public function scopeCity(Builder $query, string $city): Builder
    {
        return $query->where('city', 'like', '%' . $city . '%');
    }

    /**
     * 按层级查询.
     */
    public function scopeLevel(Builder $query, int $level): Builder
    {
        return $query->where('level', $level);
    }

    /**
     * 查询启用的校区.
     */
    public function scopeEnabled(Builder $query): Builder
    {
        return $query->where('status', 1);
    }

    /**
     * 获取校区下的老师.
     */
    public function teachers(): HasMany
    {
        return $this->hasMany(Teacher::class, 'campus_id', 'id')
            ->where('status', 1)
            ->whereNull('deleted_at');
    }

    protected function getParentIdField(): string
    {
        return 'parent_id';
    }

    protected function getSortField(): string
    {
        return '';
    }
}
